/*
 * FFmpegMediaMetadataRetriever: A unified interface for retrieving frame
 * and meta data from an input media file.
 *
 * Copyright 2016 William Seemann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FFMPEG_MEDIAMETADATARETRIEVER_H_
#define FFMPEG_MEDIAMETADATARETRIEVER_H_

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/dict.h>

typedef enum {
    OPTION_PREVIOUS_SYNC = 0,
    OPTION_NEXT_SYNC = 1,
    OPTION_CLOSEST_SYNC = 2,
    OPTION_CLOSEST = 3,
} Options;

typedef struct State {
    AVFormatContext *pFormatCtx;
    int audio_stream;
    int video_stream;
    AVStream *audio_st;
    AVStream *video_st;
    int fd;
    int64_t offset;
    const char *headers;
    struct SwsContext *sws_ctx;
    AVCodecContext *codecCtx;
    struct SwsContext *scaled_sws_ctx;
    AVCodecContext *scaled_codecCtx;
    void *callback_data_source;
    int (*media_data_source_callback)(void *, uint8_t *, int);
    int64_t (*media_data_source_seek_callback)(void *opaque, int64_t offset, int whence);
    int64_t position;
} State;

struct AVDictionary {
    int count;
    AVDictionaryEntry *elems;
};

int set_data_source(State **ps, const char *path);
int get_frame_at_time(State **ps, int64_t timeUs, int option, AVPacket *pkt);
int get_scaled_frame_at_time(State **ps, int64_t timeUs, int option, AVPacket *pkt, int width, int height);
AVPacket get_packet(State **state, char *timeUs, char *option);
void releaseFrame(State **ps);

#endif /*FFMPEG_MEDIAMETADATARETRIEVER_H_*/
